package com.imnc.foodmap;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;

/**
 * @author liuhuan
 * @version 1.0
 * @date 2014-4-21 下午6:21:12
 * 
 * 程序入口
 */
public class BaseApplication extends Application {

	private BMapManager mBMapManager = null;
	public boolean m_bKeyRight = true;
	private static BaseApplication mInstance = null;

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		initEngineManager(getApplicationContext());
	}

	private void initEngineManager(Context context) {
		if (mBMapManager == null) {
			mBMapManager = new BMapManager(context);
		}

		if (!mBMapManager.init(new EatMapMKGeneralListener())) {
			Toast.makeText(
					BaseApplication.getInstance().getApplicationContext(),
					"BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
		}
	}

	public static class EatMapMKGeneralListener implements MKGeneralListener {

		@Override
		public void onGetNetworkState(int iError) {
			if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
				Toast.makeText(
						BaseApplication.getInstance().getApplicationContext(),
						"您的网络出错啦！", Toast.LENGTH_LONG).show();
			} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
				Toast.makeText(
						BaseApplication.getInstance().getApplicationContext(),
						"输入正确的检索条件！", Toast.LENGTH_LONG).show();
			}
		}

		@Override
		public void onGetPermissionState(int iError) {
			// 非零值表示key验证未通过
			if (iError != 0) {
				// 授权Key错误：
				Toast.makeText(
						BaseApplication.getInstance().getApplicationContext(),
						"请在 DemoApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常！error: "
								+ iError, Toast.LENGTH_LONG).show();
				BaseApplication.getInstance().m_bKeyRight = false;
			} else {
				BaseApplication.getInstance().m_bKeyRight = true;
				Toast.makeText(
						BaseApplication.getInstance().getApplicationContext(),
						"key认证成功", Toast.LENGTH_LONG).show();
			}
		}

	}

	public static BaseApplication getInstance() {
		return mInstance;
	}

	public BMapManager getmBMapManager() {
		return mBMapManager;
	}

	public void setmBMapManager(BMapManager mBMapManager) {
		this.mBMapManager = mBMapManager;
	}

	public boolean isM_bKeyRight() {
		return m_bKeyRight;
	}

	public void setM_bKeyRight(boolean m_bKeyRight) {
		this.m_bKeyRight = m_bKeyRight;
	}

}
